package com.leo.component;

import com.almasb.fxgl.dsl.FXGL;
import com.almasb.fxgl.entity.component.Component;
import com.almasb.fxgl.texture.Texture;
import javafx.beans.property.SimpleDoubleProperty;
import javafx.scene.layout.StackPane;
import javafx.scene.shape.Rectangle;

/**
 * @Author: wangss
 * @CreateTime: 2023-06-08  22:49
 * @Description: 射击进度条
 */
public class ShootBarComponent extends Component {

    private SimpleDoubleProperty tpfTotal = new SimpleDoubleProperty();

    @Override
    public void onAdded() {

        Texture bar = FXGL.texture("bar.png");
        StackPane stackPane = new StackPane(FXGL.texture("bar_bg.png"), bar);

        // 遮罩层
        double barWidth = bar.getWidth();
        System.out.println(barWidth);
        Rectangle rect = new Rectangle(barWidth, bar.getHeight());
        rect.setTranslateX(-barWidth);
        // 设置遮罩层
        // clip是裁剪的意思，这里的意思是 bar使用 rect进行裁剪，也就是bar和rect重叠的部分才会展示出来
        // 所以当rect从左往右移动时，会慢慢和bar重叠，bar才会慢慢展示
        bar.setClip(rect);

        entity.getViewComponent().addChild(stackPane);

        tpfTotal.addListener((observable, oldValue, newValue) -> {
            rect.setTranslateX(-barWidth + Math.min(1.0, newValue.doubleValue() / 0.5) * barWidth);
        });
    }

    @Override
    public void onUpdate(double tpf) {
        tpfTotal.set(tpfTotal.get() + tpf);
    }

    public void reset() {
        tpfTotal.set(0);
    }
}
